/ Home / HowTo / ConnectionStringBuilder

The LJCDataAccess.DataAccess class is capable of connecting using the SQL Server data provider, OLEDB driver, ODBC driver or MySQL data provider.

SQL Server

using LJCDataAccess;
using System.Data.Common;

// Create DataAccess object.
private static DataAccess CreateDataAccess()
{
  // Create Data Configuration values.
  var connectionBuilder = new DbConnectionStringBuilder()
  {
    { "Data Source", "DataServerName" },
    { "Initial Catalog", "DatabaseName" },
    { "Integrated Security", "True" }
    // { "User Id", "UserID" },
    // { "Password", "Password" }
  };
  var connectionString = connectionBuilder.ConnectionString;
  var providerName = "System.Data.SqlClient";

  // Create DataAccess object.
  var retValue = new DataAccess(connectionString, providerName);
  return retValue;
}

MySQL

  var connectionBuilder = new DbConnectionStringBuilder()
  {
    { "server", "DataServerName" },
    { "database", "DatabaseName" }
    { "UserId", "UserID" },
    { "Password", "Password" }
  };
  var connectionString = connectionBuilder.ConnectionString;
  var providerName = "MySql.Data.MySqlClient";

The following are possible Connection Strings. See your provider documentation for the latest information.

OLEDB

  var connectionBuilder = new DbConnectionStringBuilder()
  {
    { "Provider", "SQLOLEDB" },
    { "Data Source", "DataServerName" },
    { "Initial Catalog", "DatabaseName" },
    // { "User Id", "UserID" },
    // { "Password", "Password" }
  };
  var connectionString = connectionBuilder.ConnectionString;
  var providerName = "SQLOLEDB";

ODBC

  var connectionBuilder = new DbConnectionStringBuilder()
  {
    { "Driver", "SQL Native Client" },
    { "Server", "DataServerName" },
    { "Database", "DatabaseName" },
    { "uid", "UserID" },
    { "pwd", "Password" }
  };
  var connectionString = connectionBuilder.ConnectionString;
  var providerName = "MSDASQL.1";

Microsoft Access

  var connectionBuilder = new DbConnectionStringBuilder()
  {
    { "Provider", "Microsoft.ACE.OLEDB.12.0" },
    { "Data Source", "C:\File.accdb" },
    { "Persist Security Info", "False" },
    { "Jet OLEDB:Database Password", "Password" }
  };
  var connectionString = connectionBuilder.ConnectionString;
  var providerName = "Microsoft.Jet.OLEDB.4.0";

Excel 12

  var connectionBuilder = new DbConnectionStringBuilder()
  {
    { "Data Source", "C:\File.xlsx" },
    { "Extended Properties", "Excel 12.0" },
    { "HDR", "YES" }
  };
  var connectionString = connectionBuilder.ConnectionString;

Excel 8

  var connectionBuilder = new DbConnectionStringBuilder()
  {
    { "Data Source", "C:\File.xls" },
    { "Extended Properties", "Excel 8.0" },
    { "HDR", "YES" }
  };
  var connectionString = connectionBuilder.ConnectionString;

Oracle

  var connectionBuilder = new DbConnectionStringBuilder()
  {
    { "Data Source", "ORCL" },
    { "Integrated Security", "yes" }
    // { "User Id", "UserID" },
    // { "Password", "Password" }
  };
  var connectionString = connectionBuilder.ConnectionString;

Text

  var connectionBuilder = new DbConnectionStringBuilder()
  {
    { "Provider", "Microsoft.ACE.OLEDB.12.0" },
    { "Data Source", "C:\File.txt" },
    { "Extended Properties", "text" },
    { "HDR", "yes" },
    { "FMT", "Delimited" }
  };
/ Home / HowTo / ConnectionStringBuilder

Copyright © Lester J. Clark and Contributors.
Licensed under the MIT License.